<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Intervention\Image\ImageManagerStatic as Image;

class NewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
       //保存搜索条件
        $where = [];
        //实例化要操作的表
        $db = DB::table('news');
        //判断有没有搜索name字段的值
        // dd();
        if($request->has('title')){
            // 获取要搜索的name字段的值
            $title = $request->input('title');
            //添加where模糊匹配条件
            $db->where('title','like',"%{$title}%");
            $where['title'] = $title;
        }
        // 分页
        $list = $db->paginate(10);
        // $list = $db->get();
        //加载模板
        return view('news.index',['list'=>$list,'where'=>$where]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('news.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //验证规则
         $messages = [
            'required' => ':attribute 是必填的',
        ];
        $this->validate($request, [
            'title' => 'required|max:16',
            
            // 'images'=>'required',
            
            
        ],$messages);
        // dd($request);
        $data = $request->except('_token');
        $file1=$request->file('images');
        // dd($file1);
        
         $ext1 = $file1->getClientOriginalExtension();
         $picname1 = time().rand(1000,9999).'.'.$ext1;
         $data['images']=$picname1;
          $id = DB::table('news')->insertGetId($data);
          if($id>0){
             $file1->move('./images/',$picname1);
              return redirect('/admin/news')->with('msg','添加成功');
          }else{
            return redirect('/admin/news')->with('msg','添加失败');
          }
           
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        
         $news= DB::table('news')->where('id',$id)->get()->all();
       
        // dd($product);
        return view('news.edit',['news'=>$news,'id'=>$id]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         //验证规则
         $messages = [
            'required' => ':attribute 是必填的',
        ];
        $this->validate($request, [
            'title' => 'required|max:16',
            
            // 'images'=>'required',
            
            
        ],$messages);
        // dd($request);
        $data = $request->except('_token','_method');
        $file1=$request->file('images');
        // dd($file1);
        
         $ext1 = $file1->getClientOriginalExtension();
         $picname1 = time().rand(1000,9999).'.'.$ext1;
         $data['images']=$picname1;
          $id = DB::table('news')->update($data);
          if($id>0){
             $file1->move('./images/',$picname1);
              return redirect('/admin/news')->with('msg','修改成功');
          }else{
            return redirect('/admin/news')->with('msg','修改失败');
          }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $des=DB::table('news')->where('id',$id)->delete();
        if($des>0){
            return redirect('/admin/news')->with('msg','删除成功');
        }else{
            return redirect('/admin/news')->with('msg','删除失败');
        }
    }
}
